#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::Dialog)
{
    m_ui->setupUi(this);
    connect(this,SIGNAL(accepted()),this,SLOT(on_Dialog_accepted()));
}

Dialog::~Dialog()
{
    delete m_ui;
}

void Dialog::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Dialog::setMatrix(CorrelationMatrix *matr)
{
    cmatrix = matr;
    updateView();
}

void Dialog::setAnatomicalTemplate(AnatomicalTemplate *temp)
{
    aTemplate = temp;
    updateView();
}

CorrelationMatrix* Dialog::matrix()
{
    //const CorrelationMatrix matr = cmatrix;
    //const CorrelationMatrix matr(cmatrix);
    //return matr;
    return cmatrix;
}

void Dialog::updateView()
{
    int h = cmatrix->height();
    int w = cmatrix->width();
    m_ui->tableWidget->setRowCount(h);
    m_ui->tableWidget->setColumnCount(w);

    for(int i = 0;i < h;i++)
    {
        for(int j = 0;j < w;j++)
        {
            double d = cmatrix->at(i,j);
            char dstr[50];
            m_ui->tableWidget->setItem(i,j,new QTableWidgetItem(QString(dstr)));
            m_ui->tableWidget->setColumnWidth(j,50);
        }
    }

    QStringList list;

    for(int i = 0;i<aTemplate->getNumberOfROIs();i++)
        list.append(tr(aTemplate->getRegion(i)->info().ShortName.c_str()));

    m_ui->tableWidget->setHorizontalHeaderLabels(list);
    m_ui->tableWidget->setVerticalHeaderLabels(list);

}

void Dialog::on_Dialog_accepted()
{
    /*for(int i = 0;i < m_ui->tableWidget->rowCount();i++)
    {
        for(int j = 0; j < m_ui->tableWidget->columnCount(); j++)
        {
            bool ok;
            double d = m_ui->tableWidget->item(i,j)->text().toDouble(&ok);
            if(ok)
                cmatrix.set(i,j,d);
        }
    }*/
}

void Dialog::on_tableWidget_cellChanged(int row, int column)
{
    bool ok;
    double d = m_ui->tableWidget->item(row,column)->text().toDouble(&ok);
    if(ok)
        cmatrix->set(row,column,d);
}
